$(function(){
    
    $(document).ready(function() {
         $('#search input').autocomplete2({
			appendTo: '#search > div',
			width: '100%',
			serviceUrl: 'index.php?route=module/search',
			paramName: 'search',
			onSelect: function (suggestion) {
				location = suggestion.url;
			},
			transformResult: function (response) {
				response = $.parseJSON(response);
				return {
					suggestions: $.map(response.results, function (dataItem) {
						return { value: dataItem.name, data: dataItem, image: dataItem.image, price: dataItem.price, special: dataItem.special, url: dataItem.url };
					})
				};
			},
			formatResult: function (suggestion, currentValue) {
				var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g'),
					pattern = '(' + currentValue.replace(reEscape, '\\$1') + ')',
					name = suggestion.value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
				var html = '<a href="' + suggestion.url + '">';
				if (suggestion.image) {
					html += '<span class="p-image xs-33 sm-33 md-33 lg-33 xl-33"><img src="' + suggestion.image + '" /></span>';
				}
				html += '<span class="p-name xs-66 sm-66 md-66 lg-66 xl-66"><span>' + name + '</span>';
				if (suggestion.price) {
					if (suggestion.special) {
						html += '<span class="p-price xs-66 sm-66 md-66 lg-66 xl-66"><span class="price-old">' + suggestion.price + '</span><span class="price-new">' + suggestion.special + '</span></span>';
					} else {
						html += '<span class="p-price xs-66 sm-66 md-66 lg-66 xl-66">' + suggestion.price + '</span>';
					}
				}
				html += '</span>';
				html += '<div class="clearfix"> </div>';
				html += '</a>';
				return html;
			}
		});
    });
    
   
}());